package com.zmn.plat.business.impl.sendtemplate;

import java.util.Objects;

import com.zmn.common.dto.ResultDTO;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.manager.mq.utils.MQPushUtil;
import com.zmn.plat.business.interfaces.sendtemplate.SendSmsInfoBService;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.manager.constant.ZmnSmsConsts;
import com.zmn.plat.model.entity.message.MsgTemplate;
import com.zmn.plat.model.entity.push.ZmnSms;

import lombok.extern.slf4j.Slf4j;

/**
 * 描述：通过掌上通发送消息
 *
 * @author wangjie
 * @since 2020/6/19 10:48
 **/
@Slf4j
public class ZSTSendSmsInfoBServiceImpl implements SendSmsInfoBService {

    private static final String TAG = "通过掌上通短信发送消息";

    private volatile static ZSTSendSmsInfoBServiceImpl zmnZSTSendSmsInfoBService;

    private ZSTSendSmsInfoBServiceImpl() {}

    @Override
    public Integer doSend(MsgTemplate msgTemplate, ZmnSms zmnSms) {

        // 验证必要条件
        if (StringUtil.isBlank(msgTemplate.getMessage())) {
            log.error("[{}],发送失败，短信内容不能为null。 code:{},msg:{}", TAG, PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_ZST_SMS_CONTENT_NONE, ZmnSmsConsts.MSG_NO_TEMPLATE_MESSAGE_ERROR_STR);
            // return ZmnSmsConsts.MSG_NO_TEMPLATE_MESSAGE_ERROR;
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_ZST_SMS_CONTENT_NONE);
        }
        if (StringUtil.isBlank(zmnSms.getMobile())) {
            log.error("[{}],发送失败，接收电话不能为null。 code:{},msg:{}", TAG, PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_ZST_SMS_MOBILE_NONE, ZmnSmsConsts.MSG_NO_TEMPLATE_MESSAGE_ERROR_STR);
            // return ZmnSmsConsts.MSG_NO_TEMPLATE_MESSAGE_ERROR;
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_ZST_SMS_MOBILE_NONE);
        }
        ResultDTO result = MQPushUtil.sendBizSmsInfo(zmnSms.getMobile(), msgTemplate.getMessage());
        log.info("[{}],发送参数。mobile：{}，msg:{}", TAG, zmnSms.getMobile(), msgTemplate.getMessage());

        if (result.getStatusBool()) {
            log.info("[{}],发送成功。 code:{},msg:{}", TAG, ZmnSmsConsts.MSG_SEND_SUCCESS, ZmnSmsConsts.MSG_SEND_SUCCESS_STR);
            return ZmnSmsConsts.MSG_SEND_SUCCESS;
        } else {
            log.info("[{}],发送失败。 code:{},msg:{}", TAG, PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_ZST_SMS_SEND_ERROR, result.getMessage());
            // return ZmnSmsConsts.MSG_SEND_ZST_TERROR;
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_ZST_SMS_SEND_ERROR);
        }
    }

    /**
     * 描述：获取该类的单例对象
     *
     * @return com.zmn.plat.business.impl.sendtemplate.ZmnZSTSendSmsInfoBServiceImpl
     * @author wangjie
     * @since 2020/6/19 10:49
     **/
    public static ZSTSendSmsInfoBServiceImpl getSingleton() {
        if (Objects.isNull(zmnZSTSendSmsInfoBService)) {
            synchronized (ZSTSendSmsInfoBServiceImpl.class) {
                if (Objects.isNull(zmnZSTSendSmsInfoBService)) {
                    zmnZSTSendSmsInfoBService = new ZSTSendSmsInfoBServiceImpl();
                }
            }
        }
        return zmnZSTSendSmsInfoBService;
    }
}
